/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author couett'
 */
@FacesConverter("calendarConverter")
public class CalendarConverter extends DateTimeConverter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value.equals(""))
            return null;
        String[] var = value.split("/");
        
        
        GregorianCalendar gc = new GregorianCalendar(new Integer(var[2]), new Integer(var[1]), new Integer(var[0]));
        return gc.getTime();
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        return sdf.format(((GregorianCalendar)value).getTime());
    }

}
